from django.http import JsonResponse
from django.shortcuts import render
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.views import APIView
from django.http import Http404
from article.models import Article, Category, Tag, Avatar
from article.serializers import ArticleDetailSerializer, ArticleSerializer, CategorySerializer
# from article.serializers import  ArticleListSerializer
from rest_framework import mixins
from rest_framework import generics
from rest_framework.permissions import IsAdminUser, SAFE_METHODS
from rest_framework import permissions
from rest_framework import viewsets
from rest_framework import filters
from article.serializers import CategorySerializer, CategoryDetailSerializer, TagSerializer, AvatarSerializer


class IsAdminUserOrReadOnly(IsAdminUser):

    def has_permission(self, request, view):
        # is_admin = super(
        #     IsAdminUserOrReadOnly,
        #     self).has_permission(request, view)
        is_admin = super().has_permission(request, view)
        return request.method in SAFE_METHODS or is_admin


class ArticleViewSet(viewsets.ModelViewSet):
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer
    permission_classes = [IsAdminUserOrReadOnly]
    filter_backends = [filters.SearchFilter]
    search_fields = ['title']

    def perform_create(self, serializer):
        serializer.save(author=self.request.user)

    def get_queryset(self):
        queryset = self.queryset
        username = self.request.query_params.get('username', None)
        if username is not None:
            queryset = queryset.filter(author__username=username)

        return queryset

    # 新增 get_serializer_class() 方法
    def get_serializer_class(self):
        if self.action == 'list':
            return ArticleSerializer
        else:
            return ArticleDetailSerializer


class CategoryViewSet(viewsets.ModelViewSet):
    """分类视图集"""
    queryset = Category.objects.all()
    serializer_class = CategorySerializer
    permission_classes = [IsAdminUserOrReadOnly]

    def get_serializer_class(self):
        if self.action == 'list':
            return CategorySerializer
        else:
            return CategoryDetailSerializer

    # def get_serializer_class(self):
    #     if self.action == 'list':
    #         return SomeSerializer
    #     else:
    #         return AnotherSerializer


# class ArticleList(generics.ListCreateAPIView):
#     """博客列表"""
#     queryset = Article.objects.all()
#     serializer_class = ArticleSerializer
#     permission_classes = [IsAdminUserOrReadOnly]
#
#     def perform_create(self, serializer):
#         serializer.save(author=self.request.user)


class IsAdminUserOrReadOnly(permissions.BasePermission):
    """
    仅管理员用户可进行修改
    其他用户仅可查看
    """

    def has_permission(self, request, view):
        # 对所有人允许 GET, HEAD, OPTIONS 请求
        if request.method in permissions.SAFE_METHODS:
            return True

        # 仅管理员可进行其他操作
        return request.user.is_superuser


class ArticleDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Article.objects.all()
    serializer_class = ArticleDetailSerializer
    permission_classes = [IsAdminUserOrReadOnly]


class TagViewSet(viewsets.ModelViewSet):
    queryset = Tag.objects.all()
    serializer_class = TagSerializer
    permission_classes = [IsAdminUserOrReadOnly]


class AvatarViewSet(viewsets.ModelViewSet):
    queryset = Avatar.objects.all()
    serializer_class = AvatarSerializer
    permission_classes = [IsAdminUserOrReadOnly]
